package com.stuManSystem.views;

import com.stuManSystem.base.BaseDao;
import com.stuManSystem.config.AppConstants;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import com.stuManSystem.config.Dao;
import com.stuManSystem.dao.StudentDao;
import com.stuManSystem.model.Student;
import sun.swing.table.DefaultTableCellHeaderRenderer;

import static com.stuManSystem.config.AppConstants.*;


public class MainView extends JFrame {

    private JButton jButtonFirst, jButtonLast, jButtonNext, jButtonPre, jButtonAdd, jButtonDelete, jButtonUpdate,
            jButtonFind;
    private JTextField condition;
    private JLabel currPageNumJLabel;
    private JPanel jPanelNorth, jPanelSouth, jPanelCenter;
    private DefaultTableModel myTableModel;
    private int currentPageNum=1;
    private int maxPageNum=99;

    public static JTable jTable;
    private JScrollPane jScrollPane;

    public static String[] column = { "id", AppConstants.STUDENT_NAME, AppConstants.STUDENT_SNO,
            AppConstants.STUDENT_SEX, AppConstants.STUDENT_DEPARTMETN, AppConstants.STUDENT_HOMETOWN,
            AppConstants.STUDENT_MARK, AppConstants.STUDENT_EMAIL, AppConstants.STUDENT_TEL };

    StudentDao studentDao=new StudentDao();
    String[][] result;
    public MainView(){
        init();
    }

     public void init(){
        setTitle(AppConstants.MAINVIEW_TITLE);
         condition = new JTextField();
         jButtonFind = new JButton(AppConstants.PARAM_FIND);
         jButtonAdd = new JButton(AppConstants.PARAM_ADD);
         jButtonDelete = new JButton(AppConstants.PARAM_DELETE);
         jButtonUpdate = new JButton(AppConstants.PARAM_UPDATE);
         jButtonFirst = new JButton(AppConstants.MAINVIEW_FIRST);
         jButtonPre = new JButton(AppConstants.MAINVIEW_PRE);
         jButtonNext = new JButton(AppConstants.MAINVIEW_NEXT);
         jButtonLast = new JButton(AppConstants.MAINVIEW_LAST);
         currPageNumJLabel=new JLabel(MAINVIEW_PAGENUM_JLABEL_DI+currentPageNum+ MAINVIEW_PAGENUM_JLABEL_YE);
         currPageNumJLabel.setHorizontalAlignment(JLabel.CENTER);
         
         jPanelNorth = new JPanel();
         jPanelNorth.setLayout(new GridLayout(1,5));
         jPanelNorth.add(condition);
         jPanelNorth.add(jButtonFind);
         jPanelNorth.add(jButtonAdd);
         jPanelNorth.add(jButtonDelete);
         jPanelNorth.add(jButtonUpdate);



         jPanelCenter = new JPanel();
         jPanelCenter.setLayout(new GridLayout(1,1));

         result= ((StudentDao)BaseDao.getAbilityDAO(Dao.StudentDao)).list(currentPageNum);
         //result = studentDao.list(currentPageNum);
         myTableModel = new DefaultTableModel(result,column);

         jTable = new JTable(myTableModel);
         DefaultTableCellHeaderRenderer cr=new DefaultTableCellHeaderRenderer();
         cr.setHorizontalTextPosition(JLabel.CENTER);
         jTable.setDefaultRenderer(Object.class,cr);

         initJTable(jTable,result);
         jScrollPane=new JScrollPane(jTable);
         jPanelCenter.add(jScrollPane);

         jPanelSouth = new JPanel();
         jPanelSouth.setLayout(new GridLayout(1,5));
         jPanelSouth.add(jButtonFirst);
         jPanelSouth.add(jButtonNext);
         jPanelSouth.add(currPageNumJLabel);
         jPanelSouth.add(jButtonPre);
         jPanelSouth.add(jButtonLast);

         //查询
         jButtonFind.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {

                 //获取输入框中的内容
                 String param=condition.getText();
                 //queryByName()
                 result=studentDao.queryByName(param);
                 initJTable(jTable,result);
                 dispose();
             }
         });
         //添加
         jButtonAdd.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {
                  AddView addView=new AddView();
                //  result=studentDao.add(addView);
                 dispose();

             }

         });
         //删除
         jButtonDelete.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {
                    new DeleteView();
                    dispose();
             }
         });
         //更新
         jButtonUpdate.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {
                 new UpdateView();
             }
         });
         //首页
         jButtonFirst.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {
                 currentPageNum=1;
                 result=studentDao.list(currentPageNum);
                 initJTable(jTable,result);
                 currPageNumJLabel.setText(MAINVIEW_PAGENUM_JLABEL_DI+currentPageNum+ MAINVIEW_PAGENUM_JLABEL_YE);

             }
         });
         //上一页
         jButtonPre.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {

                 if(currentPageNum>1){
                     currentPageNum--;
                     result=studentDao.list(currentPageNum);
                     initJTable(jTable,result);
                     currPageNumJLabel.setText(MAINVIEW_PAGENUM_JLABEL_DI+currentPageNum+ MAINVIEW_PAGENUM_JLABEL_YE);
                 }


             }
         });
         //下一页
         jButtonNext.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {
                 if(currentPageNum<maxPageNum){
                     currentPageNum++;
                     result=studentDao.list(currentPageNum);
                     initJTable(jTable,result);
                     currPageNumJLabel.setText(MAINVIEW_PAGENUM_JLABEL_DI+currentPageNum+ MAINVIEW_PAGENUM_JLABEL_YE);

                 }

             }
         });
         //末页
         jButtonLast.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {

                 currentPageNum=maxPageNum;
                 result=studentDao.list(currentPageNum);
                 //result= StudentDao.list(currentPageNum);
                 initJTable(jTable,result);
                 currPageNumJLabel.setText(MAINVIEW_PAGENUM_JLABEL_DI+currentPageNum+ MAINVIEW_PAGENUM_JLABEL_YE);
             }
         });


         this.add(jPanelNorth,BorderLayout.NORTH);
        this.add(jPanelCenter,BorderLayout.CENTER);
        this.add(jPanelSouth,BorderLayout.SOUTH);

        //设置出现的位置和大小
         this.setBounds(450,250,675,340);
         //窗体对象，默认是不可见的。通过设置setVisible，使其可见。
        this.setVisible(true);
        //拒绝改变窗体大小
        this.setResizable(false);
        //设置关闭窗体时的运行效果
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

    }

    //适配信息总面板
    public static void initJTable(JTable jTable, String[][] result) {
        //设置表头和数据
        ((DefaultTableModel) jTable.getModel()).setDataVector(result, column);

        //设置行高
        jTable.setRowHeight(20);
        //固定列宽
        TableColumn firsetColumn = jTable.getColumnModel().getColumn(0);
        firsetColumn.setPreferredWidth(30);
        firsetColumn.setMaxWidth(30);
        firsetColumn.setMinWidth(30);
        TableColumn secondColumn = jTable.getColumnModel().getColumn(1);
        secondColumn.setPreferredWidth(60);
        secondColumn.setMaxWidth(60);
        secondColumn.setMinWidth(60);
        TableColumn thirdColumn = jTable.getColumnModel().getColumn(2);
        thirdColumn.setPreferredWidth(90);
        thirdColumn.setMaxWidth(90);
        thirdColumn.setMinWidth(90);
        TableColumn fourthColumn = jTable.getColumnModel().getColumn(3);
        fourthColumn.setPreferredWidth(30);
        fourthColumn.setMaxWidth(30);
        fourthColumn.setMinWidth(30);
        TableColumn seventhColumn = jTable.getColumnModel().getColumn(6);
        seventhColumn.setPreferredWidth(30);
        seventhColumn.setMaxWidth(30);
        seventhColumn.setMinWidth(30);
        TableColumn ninthColumn = jTable.getColumnModel().getColumn(8);
        ninthColumn.setPreferredWidth(90);
        ninthColumn.setMaxWidth(90);
        ninthColumn.setMinWidth(90);
    }

    public static void main(String[] args) {

        new MainView();
    }


}
